// pages/friends/createAccount/index.ts
import tran from '../../../utils/language';
import { commonRequestList } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('masterStudy'),
    _tBase: tran._t('base'),
    type: 2,
    pickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: ''
    },
    ageList: [
      {
        selectFlag: false,
        text: '18及以下',
        value: '0-18'
      },
      {
        selectFlag: false,
        text: '18-25',
        value: '18-25'
      },
      {
        selectFlag: false,
        text: '26-32',
        value: '26-32'
      },
      {
        selectFlag: false,
        text: '33-41',
        value: '33-41'
      },
      {
        selectFlag: false,
        text: '42-55',
        value: '42-55'
      },
      {
        selectFlag: false,
        text: '55及以上',
        value: '55-200'
      }
    ],
    region: {},
    formData: {
      interestIds: '',
      professionId: '',
      personalities: '',
      professionName: '全部',
      languages: '',
      genderText: '',
      ageText: '',
      minAge: 0,
      maxAge: 0,
      online: 'all',
      currentProvinceCode: '',
      currentCityCode: '',
      currentDistrictCode: '',
      requestPlayText: '',
      requestPlayIds: '',
      friend: false,
      languageText: '',
      helpText: '',
      requestHelpIds: '',
    },
    characterTypeList: ['ENTP', 'INTJ', 'ENTJ', 'ENFP', 'INFP'],
    ageRangeList: [
      ['15', '20', '25', '30'],
      ['25', '30', '35', '40'],
    ],
    multiIndex: [0, 0],
    isEdit: getApp().globalData.profileFrom === 'edit',
    filterData: [{ text: '全部', key: 'all', selectFlag: true }, { text: '仅在线', key: 'online', selectFlag: false }],
    relationshipFilterData: [{ text: '全部', key: 'all', selectFlag: true }, { text: '仅在线', key: '321431', selectFlag: false }, { text: '新添加', key: '321000', selectFlag: false }],
    additionalFlag: false,
    knowledgeSelectList: [
      {
        text: '金融',
        key: '123'
      },
      {
        text: '会计',
        key: '456'
      },
      {
        text: '税务',
        key: '789'
      }
    ],
    commonPickerAgeParams: {
      show: false,
      pickerType: 'age',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    pickerParams2: {
      show: false,
      pickerType: 'skill',
      defaultValue: '',
      title: '职业'
    },
    commonPickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    helpParams: {
      show: false,
      pickerType: 'help',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    // helpList: ['帮助我提交纳税申报表', '帮助我写申请表'],
    helpList: [],
    commonPickerData: [] as commonPickerDataType[],
    langList: [],

    commonPickerType: '',
  },
  additionalTap() {
    this.setData({
      additionalFlag: !this.data.additionalFlag
    })
  },
  searchFn () {
    const { formData } = this.data
    if (!formData.requestHelpIds) {
      wx.showToast({
        title: '请选择需要什么帮助',
        icon: 'none'
      })
      return false
    }
    const infoStr = JSON.stringify(formData)
    wx.navigateTo({
      url: `/pages/profile/friendResult/index?info=${infoStr}&type=2`
    })
  },
  // 删除某一种选中的选项
  delTap(e: any) {
    const { knowledgeSelectList } = this.data
    const { index } = e.currentTarget.dataset
    // 删除选中的下标的一项元素
    knowledgeSelectList.splice(index, 1)
    this.setData({
      knowledgeSelectList
    })
  },

  /**
   * 获取寻求帮助列表
   */
  getHelpList() {
    const params = {
      requestType: 'HELP'
    }
    commonRequestList(params).then(res => {
      res.length > 0 && res.map((item: any) => {
        item.text = item.reqName
        item.value = item.reqTypeId
      })
      this.setData({
        helpList: res
      })
    })
  },
  toFriendSelectPage(e: any) {
    const currentIndex = e.target.dataset.index
    const { relationshipFilterData, formData } = this.data
    const flag = false;
    relationshipFilterData.length > 0 && relationshipFilterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
        if (currentIndex == 1) {
          formData.friend = true
        } else if (currentIndex == 2) {
          formData.friend = false
        } else {
          formData.friend = null
        }
      } else {
        item.selectFlag = false
      }

    })
    this.setData({
      relationshipFilterData,
      formData
    })
  },
  toSelectPage(e: any) {
    const currentIndex = e.target.dataset.index
    const { filterData } = this.data
    filterData.length > 0 && filterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
      } else {
        item.selectFlag = false
      }
    })
    this.setData({
      filterData
    })
  },
  handlePicker(e: baseEvent) {
    // 打开picker 设置对应的picker弹出类型
    const type = e.currentTarget.dataset.type;
  },
  pickerCancel(e: { detail: any }) {
    if (e.detail) {
      // 有值表示是确认
      this.setData({ formData: { ...this.data.formData, ...e.detail } })
    }
    this.setData({ pickerParams: { show: false, pickerType: '', defaultValue: '' } })
  },
  bindMultiPickerColumnChange(e: any,) {
    let columnIndex = e.detail.column
    let valueIndex = e.detail.value
    let { multiIndex } = this.data
    if (columnIndex == 0) {
      multiIndex[0] = valueIndex
    } else {
      multiIndex[1] = valueIndex
    }
    this.setData({
      multiIndex
    })
  },
  helpChange(e: any) {
    const { formData, helpList } = this.data
    const index = e.detail.value
    formData.help = helpList[index]
    this.setData({
      formData
    })
  },
  partnerTypeChange(e: any) {
    const { formData, partnerTypeList } = this.data
    const index = e.detail.value
    formData.partnerType = partnerTypeList[index]
    this.setData({
      formData
    })
  },
  characterTypeChange(e: any) {
    const { formData, characterTypeList } = this.data
    const index = e.detail.value
    this.setData({
      formData
    })
  },
  bindRegionChange(e: any) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getLangList()
    this.getProfession()
    this.getHelpList()
  },

  /**
   * 从app.ts里面获取全局的语言列表
   */
  getLangList() {
    let list = getApp().globalData.langList
    let langList: any = [
      {
        selectFlag: false,
        text: '全部',
        value: 'all'
      }
    ]
    list.length > 0 && list.map((item: any) => {
      const obj = {
        selectFlag: false,
        ...item
      }
      langList.push(obj)
    })
    this.setData({
      langList
    })
  },
  handleHelpPicker(e: baseEvent) {
    const { helpParams, langList } = this.data
    const type = e.currentTarget.dataset.type;
    helpParams.pickerType = type
    helpParams.show = true
    this.setData({ helpParams, commonPickerData: langList, commonPickerType: type })
  },
  handleCommonPicker(e: baseEvent) {
    const { commonPickerParams, langList } = this.data
    const type = e.currentTarget.dataset.type;
    commonPickerParams.pickerType = type
    commonPickerParams.show = true
    this.setData({ commonPickerParams, commonPickerData: langList, commonPickerType: type })
  },
  handleCommonSkillPicker(e: baseEvent) {
    const { pickerParams2 } = this.data
    const type = e.currentTarget.dataset.type;
    pickerParams2.pickerType = type
    pickerParams2.show = true
    this.setData({ pickerParams2, commonPickerType: type })
  },
  handleCommonAgePicker(e: baseEvent) {
    const { commonPickerAgeParams } = this.data
    // 打开picker 设置对应的picker弹出类型
    const type = e.currentTarget.dataset.type;
    commonPickerAgeParams.pickerType = type
    commonPickerAgeParams.show = true
    this.setData({ commonPickerAgeParams, commonPickerType: type })
  },

  /**
   * 普通的选择器确认按钮
   * @param e 
   */
  commonPickerConfirm(e: any) {
    const { commonPickerType } = this.data
    const obj = e.detail.value
    const { formData } = this.data
    if (commonPickerType == 'language') { // 语言
      formData.languageText = obj.text
      formData.languages = obj.value
    } else if (commonPickerType == 'help') { // 性别
      formData.helpText = obj.text
      formData.requestHelpIds = obj.value
    } else if (commonPickerType == 'age') { // 年龄范围
      formData.ageText = obj.text
      const list = obj.value.split('-')
      formData.minAge = list[0]
      formData.maxAge = list[1]
    } else if (commonPickerType == 'skill') { // 技能
      formData.professionName = obj.text
      formData.professionId = obj.value
    }
    this.setData({
      formData
    })
  },
  /**
    * 获取职业技能列表
    */
  getProfession() {
    const listAll = wx.getStorageSync('professionList')
    let list = [{
      text: '全部',
      value: null,
      selectFlag: false,
      tagTypeId: ''
    }]
    listAll.length > 0 && listAll.map((item: any) => {
      item.text = item.tagName
      item.selectFlag = false
      item.value = item.tagTypeId
      list.push(item)
    })
    this.setData({
      selectList: list,
      professionList: list
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})